Ext.BLANK_IMAGE_URL='../commons/img/s.gif';//占位1px的空白图片
var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
/**
 *自定义treecombobox组建
 * 调用示例：xtype:'treecombobox'
 *          url:'#',
 *          store:[{id:'1',text:'选项'}]
 */
    Ext.define('TreeComboBox', {
        extend: 'Ext.form.field.ComboBox',
        alias: 'widget.treecombobox',
        url: '',
        tree: {},
        textProperty: 'text',
        valueProperty: 'id',
        initComponent: function () {
            Ext.apply(this, {
                editable: false,
                queryMode: 'local',
                select: Ext.emptyFn
            });
            this.displayField = this.displayField || this.textProperty,
            this.treeid = Ext.String.format('tree-combobox-{0}', Ext.id());
            this.tpl = Ext.String.format('<div id="{0}"></div>', this.treeid);
            if (this.url) {
                var me = this;
                var store = Ext.create('Ext.data.TreeStore', {
                    root: { expanded: true },
                    proxy: { type: 'ajax', url: this.url }
                });
                this.tree = Ext.create('Ext.tree.Panel', {
                    rootVisible: false,
                    autoScroll: true,
                    height: 100,
                    width:me.width-me.labelWidth-7,
                    store: store
                });
                this.tree.on('itemclick', function (view, record) {
                    me.setValue(record);
                    me.collapse();
                });
                me.on('expand', function () {
                    if (!this.tree.rendered) {
                        this.tree.render(this.treeid);
                    }
                });
            }
            this.callParent(arguments);
        }
    });
    Ext.define('MonthComboBox',{
    	extend: 'Ext.form.FieldContainer',
        alias: 'widget.monthcombobox',
        requires:['Ext.data.*'],
        editable:false,
        allowBlank:false,
        initComponent:function(){
        	Ext.apply(this,{
        		layout: 'hbox',
                items: [{
                    xtype: 'numberfield',
                    name: 'ccyear',
                    hideLabel: true,
                    width: 70,
                    editable:this.editable,
                    value: new Date().getFullYear(),
                    allowBlank: this.allowBlank
                },{
                    xtype: 'combobox',
                    name: 'ccmonth',
                    displayField: 'name',
                    valueField: 'num',
                    queryMode: 'local',
                    emptyText: '月份',
                    hideLabel: true,
                    margins: '0 6 0 0',
                    editable:this.editable,
                    store: new Ext.data.Store({
                        fields: ['name', 'num'],
                        data: (function() {
                            var data = [];
                                Ext.Array.forEach(Ext.Date.monthNames, function(name, i) {
                                data[i] = {name: name, num: i + 1};
                            });
                            return data;
                        })()
                    }),
                    width: 90,
                    allowBlank: this.allowBlank,
                    forceSelection: true
                }]
        	});
        	this.callParent(arguments);
        }
    });

    Ext.define('MonthComboBox1',{
        extend: 'Ext.form.FieldContainer',
        alias: 'widget.monthcombobox1',
        requires:['Ext.data.*'],
        editable:false,
        allowBlank:false,
        initComponent:function(){
            Ext.apply(this,{
                layout: 'vbox',
                items: [{
                    xtype: 'datefield',
                    name: 'ccyear',
                    id:'cbegin',
                    hideLabel: true,
                    width: 200,
                    editable:this.editable,
                    format :'Y-m-d',
                    value: new Date(),
                    maxValue:new Date(),
                    allowBlank: this.allowBlank
                },{
                    xtype: 'datefield',
                    name: 'ccmonth',
                    id:'cend',
                    fieldLabel:'至',
                    labelWidth:40,
                    width: 200,
                    editable:this.editable,
                    format :'Y-m-d',
                    value: new Date(),
                    maxValue:new Date(),
                    allowBlank: this.allowBlank
                }]
            });
            this.callParent(arguments);
        },
        getSubmitValue : function() {
            return getValue();
        },
        getValue : function() {
            return Ext.getCmp('cbegin').getRawValue().replace('-','').substr(0,6)+'-'+Ext.getCmp('cend').getRawValue().replace('-','').substr(0,6);
        }
    });
    
Ext.define('TreePicker', {
    extend : 'Ext.form.field.Picker', //'Ext.form.field.ComboBox',
    alias : 'widget.treepicker',
    uses : ['Ext.tree.Panel'],
    valueField : 'id',
    displayField : 'text',
    editable : false,
    store : null,
    allowBlank:false,
    initComponent : function() {
        var me = this;
        me.callParent(arguments);
        me.addEvents('select');
        //添加select事件
    },
    createPicker : function() {
        var me = this, picker;
        picker = new Ext.tree.Panel({
            height : 150,
            autoScroll : true,
            store : me.store,
            floating : true,
            focusOnToFront : false,
            shadow : false,
            rootVisible : false,
            listeners : {
                scope : me,
                itemclick : me.onItemClick
            }
        });
        return picker;
    },
    onItemClick : function(view, record, node, rowIndex, e) {
        this.selectItem(record);
    },
    selectItem : function(record) {
        var me = this;
        me.setValue(record.getId());
        me.picker.hide();
        me.inputEl.focus();
        me.fireEvent('select', me, record);
    },
    setValue : function(value) {
        var me = this, record;
        me.value = value;
        if (me.store.loading) {
            me.setRawValue(value);
        } else {
            record = value ? me.store.getNodeById(value) : me.store.getRootNode();
            if (value === undefined) {
                record = me.store.getRootNode();
                me.value = record.getId();
            } else {
                record = me.store.getNodeById(value);
            }
            me.setRawValue( record ? record.get(me.displayField) : '');
        }
        return me;
    },
    getSubmitValue : function() {
        return this.value;
    },
    getValue : function() {
        return this.value;
    }
});